package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.GjhjjtbgBsDetail;
import com.ruoyi.report_gc.domain.vo.GjhjjtbgBsDetailVo;
import com.ruoyi.report_gc.mapper.GjhjjtbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.annotation.FIllBasicInfoForTaiZhang;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GjhjjtbgBsInfoBo;
import com.ruoyi.report_gc.domain.vo.GjhjjtbgBsInfoVo;
import com.ruoyi.report_gc.domain.GjhjjtbgBsInfo;
import com.ruoyi.report_gc.mapper.GjhjjtbgBsInfoMapper;
import com.ruoyi.report_gc.service.IGjhjjtbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢筋焊接接头试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-06-24
 */
@RequiredArgsConstructor
@Service
public class GjhjjtbgBsInfoServiceImpl implements IGjhjjtbgBsInfoService {

    private final GjhjjtbgBsInfoMapper baseMapper;
    private final GjhjjtbgBsDetailMapper detailMapper;
    private final TgsybgBaDetailMapper tgDetailMapper;

    private final IBiQualificationService biQualificationService;

    private static final Long[] keys = {106L, 106L, 106L, 106L, 106L, 106L};

    /**
     * 查询钢筋焊接接头试验检测报告
     */
    @Override
    @FIllBasicInfo
    public GjhjjtbgBsInfoVo queryById(Long excelId){
        GjhjjtbgBsInfoVo gjhjjtbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<GjhjjtbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GjhjjtbgBsDetail>().eq(GjhjjtbgBsDetail::getExcelId, excelId));
        gjhjjtbgBsInfoVo.setDetailList(detailList);

        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = tgDetailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
//        for(int i=0; i<baDetailVos.size(); i++){
//            if(i<=2){
//                if (!"-".equals(baDetailVos.get(i).getJiszb()) && ObjectUtils.isNotEmpty(baDetailVos.get(i).getJiancjg())){
//                    if ("不符合".equals(baDetailVos.get(i).getJiegpd())){
//                        baDetailVos.get(0).setJiegpd("不符合");
//                        break;
//                    }else {
//                        baDetailVos.get(0).setJiegpd("符合");
//                    }
//                }
//            }
//        }
//        for(int i=0; i<baDetailVos.size(); i++){
//            if(i>2){
//                if (!"-".equals(baDetailVos.get(i).getJiszb()) && ObjectUtils.isNotEmpty(baDetailVos.get(i).getJiancjg())){
//                    if ("不符合".equals(baDetailVos.get(i).getJiegpd())){
//                        baDetailVos.get(0).setJiegpd("不符合");
//                        break;
//                    }else {
//                        baDetailVos.get(0).setJiegpd("符合");
//                    }
//                }
//            }
//        }
        gjhjjtbgBsInfoVo.setBaDetailVos(baDetailVos);
        return gjhjjtbgBsInfoVo;
    }

    @Override
    @FIllBasicInfoForTaiZhang
    public GjhjjtbgBsInfoVo queryByIdForTaizhang(Long excelId) {
        GjhjjtbgBsInfoVo gjhjjtbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<GjhjjtbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GjhjjtbgBsDetail>().eq(GjhjjtbgBsDetail::getExcelId, excelId));
        gjhjjtbgBsInfoVo.setDetailList(detailList);

        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = tgDetailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        gjhjjtbgBsInfoVo.setBaDetailVos(baDetailVos);

        //台账
        for(int i = 0; i<detailList.size(); i++) {
            //极限强度台账
            if (ObjectUtils.isNotEmpty(detailList.get(i).getJiancjg())) {
                if (ObjectUtils.isEmpty(gjhjjtbgBsInfoVo.getJixqdTaizhang())) {
                    gjhjjtbgBsInfoVo.setJixqdTaizhang(detailList.get(i).getJiancjg());
                } else {
                    gjhjjtbgBsInfoVo.setJixqdTaizhang(gjhjjtbgBsInfoVo.getJixqdTaizhang() + "/" + detailList.get(i).getJiancjg());
                }
            }
            //断口型式台账
            if (ObjectUtils.isNotEmpty(detailList.get(i).getDankxs())) {
                if (ObjectUtils.isEmpty(gjhjjtbgBsInfoVo.getDankxsTaizhang())) {
                    gjhjjtbgBsInfoVo.setDankxsTaizhang(detailList.get(i).getDankxs());
                } else {
                    gjhjjtbgBsInfoVo.setDankxsTaizhang(gjhjjtbgBsInfoVo.getDankxsTaizhang() + "/" + detailList.get(i).getDankxs());
                }
            }
            //断口离焊口距离(mm)台账
            if (ObjectUtils.isNotEmpty(detailList.get(i).getDuanklhkjl())) {
                if (ObjectUtils.isEmpty(gjhjjtbgBsInfoVo.getDuanklhkjlTaizhang())) {
                    gjhjjtbgBsInfoVo.setDuanklhkjlTaizhang(detailList.get(i).getDuanklhkjl());
                } else {
                    gjhjjtbgBsInfoVo.setDuanklhkjlTaizhang(gjhjjtbgBsInfoVo.getDuanklhkjlTaizhang() + "/" + detailList.get(i).getDuanklhkjl());
                }
            }
            //弯曲结果台账
            if (ObjectUtils.isNotEmpty(detailList.get(i).getWanqjg())) {
                if (ObjectUtils.isEmpty(gjhjjtbgBsInfoVo.getWanqjgTaizhang())) {
                    gjhjjtbgBsInfoVo.setWanqjgTaizhang(detailList.get(i).getWanqjg());
                } else {
                    gjhjjtbgBsInfoVo.setWanqjgTaizhang(gjhjjtbgBsInfoVo.getWanqjgTaizhang() + "/" + detailList.get(i).getWanqjg());
                }
            }
        }

        return gjhjjtbgBsInfoVo;
    }

    /**
     * 查询钢筋焊接接头试验检测报告列表
     */
    @Override
    public TableDataInfo<GjhjjtbgBsInfoVo> queryPageList(GjhjjtbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GjhjjtbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<GjhjjtbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋焊接接头试验检测报告列表
     */
    @Override
    public List<GjhjjtbgBsInfoVo> queryList(GjhjjtbgBsInfoBo bo) {
        LambdaQueryWrapper<GjhjjtbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GjhjjtbgBsInfo> buildQueryWrapper(GjhjjtbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GjhjjtbgBsInfo> lqw = Wrappers.lambdaQuery();
//        lqw.eq(bo.getTotalPage() != null, GjhjjtbgBsInfo::getTotalPage, bo.getTotalPage());
//        lqw.eq(bo.getPage() != null, GjhjjtbgBsInfo::getPage, bo.getPage());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), GjhjjtbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), GjhjjtbgBsInfo::getBaogbh, bo.getBaogbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), GjhjjtbgBsInfo::getShigwtdw, bo.getShigwtdw());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), GjhjjtbgBsInfo::getGongcmc, bo.getGongcmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), GjhjjtbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), GjhjjtbgBsInfo::getYangpmc, bo.getYangpmc());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), GjhjjtbgBsInfo::getYangpbh, bo.getYangpbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), GjhjjtbgBsInfo::getYangpsl, bo.getYangpsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), GjhjjtbgBsInfo::getYangpzt, bo.getYangpzt());
//        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), GjhjjtbgBsInfo::getYangpsj, bo.getYangpsj());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), GjhjjtbgBsInfo::getJiancyj, bo.getJiancyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), GjhjjtbgBsInfo::getPandyj, bo.getPandyj());
//        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), GjhjjtbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
//        lqw.eq(StringUtils.isNotBlank(bo.getDaibsl()), GjhjjtbgBsInfo::getDaibsl, bo.getDaibsl());
//        lqw.eq(StringUtils.isNotBlank(bo.getJincrq()), GjhjjtbgBsInfo::getJincrq, bo.getJincrq());
//        lqw.eq(StringUtils.isNotBlank(bo.getPih()), GjhjjtbgBsInfo::getPih, bo.getPih());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), GjhjjtbgBsInfo::getJiancjl, bo.getJiancjl());
//        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), GjhjjtbgBsInfo::getFujsm, bo.getFujsm());
//        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), GjhjjtbgBsInfo::getBgFlag, bo.getBgFlag());
//        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), GjhjjtbgBsInfo::getJianc, bo.getJianc());
//        lqw.eq(StringUtils.isNotBlank(bo.getJil()), GjhjjtbgBsInfo::getJil, bo.getJil());
//        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), GjhjjtbgBsInfo::getFuh, bo.getFuh());
//        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), GjhjjtbgBsInfo::getRiq, bo.getRiq());
//        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), GjhjjtbgBsInfo::getShebIds, bo.getShebIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), GjhjjtbgBsInfo::getPandyjIds, bo.getPandyjIds());
//        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), GjhjjtbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增钢筋焊接接头试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(GjhjjtbgBsInfoBo bo) {
        GjhjjtbgBsInfo add = BeanUtil.toBean(bo, GjhjjtbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<GjhjjtbgBsDetail> details = new ArrayList<>(12);
            for (int i=1;i<=12;i++ ){
                details.add(new GjhjjtbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);

            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> tgDetails = new ArrayList<>(6);
            for (Long key : keys) {
                tgDetails.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            tgDetailMapper.insertBatch(tgDetails);
        }
        return flag;
    }

    /**
     * 修改钢筋焊接接头试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(GjhjjtbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        GjhjjtbgBsInfo update = BeanUtil.toBean(bo, GjhjjtbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), GjhjjtbgBsDetail.class));
            tgDetailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), TgsybgBaDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(GjhjjtbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getDetailList())) {
            for (GjhjjtbgBsDetailVo detailVo : bo.getDetailList()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GjhjjtbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋焊接接头试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public GjhjjtbgBsInfoVo jcjl(GjhjjtbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，该钢筋焊接件所检指标符合JGJ 18-2024《钢筋焊接及验收规程》技术要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
